package com.sanpu.kxe.business.member.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.business.member.bean.MemberPromotOutput;
import com.sanpu.kxe.business.member.contract.MemberPromotListContract;
import com.sanpu.kxe.business.member.model.MemberModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.utils.ToastUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：会员推广列表
 * Created by 薛勇军 on 2019/4/15.
 */

public class MemberPromotListPresenter implements MemberPromotListContract.Presenter {

    private MemberPromotListContract.View mView;

    private CompositeSubscription mSubscription;

    private String merchantLevel;

    public MemberPromotListPresenter(MemberPromotListContract.View mView, String merchantLevel) {
        this.mView = mView;
        this.merchantLevel = merchantLevel;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void myUserList(final int nowPage) {
        Subscription subscription = MemberModel.getInstance().myUserList(merchantLevel, nowPage)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MemberPromotOutput>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showErrorText(e);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(List<MemberPromotOutput> pointDetailOutputs) {
                        if (pointDetailOutputs != null) {
                            mView.showPromotListView(pointDetailOutputs, nowPage == 0);
                            if (pointDetailOutputs.size() < CommonConfig.PAGENUMER) {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                            }
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void findUser(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber) || phoneNumber.length()<11) {
            ToastUtil.show("请输入正确的手机号码！");
            return;
        }
        mView.showLoadingDialog(true, "搜索中...");
        Subscription subscription = MemberModel.getInstance().findUser(merchantLevel, phoneNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MemberPromotOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                        mView.showPromotView(null);
                        mView.showLoadMore(CommonConfig.LOADMOREEND);
                    }

                    @Override
                    public void onNext(MemberPromotOutput memberPromotOutput) {
                        mView.showPromotView(memberPromotOutput);
                        mView.showLoadMore(CommonConfig.LOADMOREEND);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
